<?php
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');
if($id){
$peer = Category2productAttributePeer::getInstance();
$select = $peer->select()->where('id = ?', $id);
$category_filter = $peer->fetchRow($select);
}else{$category_filter = Category2productAttributePeer::getInstance()->createRow();}
$form = $this->element;

?>
<link rel="stylesheet" media="screen" href="http://<?php echo Config::getInstance()->default->hostname; ?>/style/admin_product.css" />
<!--[if lte IE 7]><link href="/style/ie.css" rel="stylesheet" type="text/css"><![endif]-->
<form enctype="multipart/form-data" method="post" action="">
  <div>
    <?php
    /**
     * Translate some form errrors.
     *
     * @param string $key
     * @param string $error
     * @return string
     */
    function _translate($key, $error)
    {
    	$_translated = array(
			'isEmpty' => 'Поле обязательно для заполнения',
		);
	
    	if (isset($_translated[$key])) {
    		return $_translated[$key];
    	}
    	
    	return $error;
    }
    $errors = $form->getMessages();
    foreach ($errors as $key => $error) {
    	if (is_array($error)) {
    		unset($errors[$key]);
    	}

   		if (is_array($error)) {
     		foreach ($error as $k => $v) {
     			$label = $form->getElement($key)->getLabel();
     			$v = _translate($k, $v);
     			$errors[$key][$k] = "$label -- $v";
     		}
    	} else {
    		$errors[$key] = _translate($key, $error);
    	}
    }
    
    if ($errors) {
		echo "<ul class='errors'>";
		foreach ($errors as $error) {
			if (is_array($error)) {
				foreach ($error as $k => $v) {
					echo "<li>$v</li>";
				}
			} else {
				echo "<li>$error</li>";
			}
		}
		echo "</ul>";
	}

		/*Категория*/
		if($id){
			echo "<label>Категория</label>";
			$is_cat = ProductCategoryPeer::getInstance()->fetchById($category_filter['category_id']);
			echo " -> <span><b>".$is_cat['name']."</b></span>";
			echo "<input type='hidden' name='category_id' value='".$category_filter['category_id']."'>";
		}else{
			$select = $form->getElement('category_id');
			echo $select;
		}
		echo "<br>";
		echo "<br>";
	
		/*Свойства категорий*/
		$attr = DB::getConnection()->fetchAll("SELECT * FROM `product_attribute`");
		$idsAttrib = array();
		if($id){
			$categoryAttr = DB::getConnection()->fetchAll("SELECT attribute_id FROM `category2product_attribute` WHERE category_id=".$category_filter['category_id']);
			foreach($categoryAttr as $id){
				$idsAttrib[] = $id['attribute_id'];
			}
		}
		
		echo "<div style='padding:4px; width:600px;'>";
		$cheked = '';
	    $i = 0;
		foreach($attr as $item){
			if(in_array($item['id'], $idsAttrib)){
				echo "<span><label style='padding-right: 4px; margin-left: 10px;'>".$item['name']."</label><input checked='checked' type='checkbox' name='property[".$i."]' value='".$item['id']."'></span>";
			}else{
				echo "<span><label style='padding-right: 4px; margin-left: 10px;'>".$item['name']."</label><input type='checkbox' name='property[".$i."]' value='".$item['id']."'></span>";
			}
			$i++;
		}
		echo "</div>";
?>
	<br>
	<br>
	<dd id="submit-element">
		<input type="submit" name="submit" id="submit" value="Сохранить">
	</dd>